import { ApiContentMenu, ApiJson, onMessage, sendMessage } from "./message";

function getPreJson() {
  const el = document.body.firstElementChild;
  if (el.tagName !== "PRE") return {};

  try {
    return JSON.parse(el.textContent);
  } catch (error) {
    return {};
  }
}

const { swagger, openapi, info, paths, components } = getPreJson();

if (swagger || openapi || (info && (paths || components))) {
  // 告诉 background 此标签页可以开放右击菜单栏权限
  sendMessage(ApiContentMenu);

  // 监听 background 的数据发送请求
  onMessage(ApiJson, () => {
    // 发送数据给 background
    sendMessage(ApiJson, getPreJson());
  });
}
